package com.bupt.liutong.admin.core.dto;

import java.io.File;
import java.io.IOException;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

public class TreeParser {
	
	public static TreeConfig loadConfig(String configPath) {
		Digester digester = new Digester();
		digester.setValidating(false);
		digester.push(new TreeConfig());

		// CodeTable
		digester.addObjectCreate("trees/tree", Tree.class);
		digester.addSetProperties("trees/tree");
		digester.addSetNext("trees/tree", "addTree");

		digester.addObjectCreate("trees/tree/root", TreeRoot.class);
		digester.addSetProperties("trees/tree/root");
		digester.addSetNext("trees/tree/root", "setTreeRoot");

		digester.addObjectCreate("trees/tree/category", TreeCategory.class);
		digester.addSetProperties("trees/tree/category");
		digester.addSetNext("trees/tree/category", "setTreeCategory");

		digester.addObjectCreate("trees/tree/node", TreeNode.class);
		digester.addSetProperties("trees/tree/node");
		digester.addSetNext("trees/tree/node", "setTreeNode");

		digester.addObjectCreate("trees/tree/sqlMap", TreeSqlMap.class);
		digester.addSetProperties("trees/tree/sqlMap");
		digester.addSetNext("trees/tree/sqlMap", "addSqlMap");
		
		try {
			return (TreeConfig) digester.parse(new File(configPath));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return null;
	}
}
